package com.agp.utils;

import com.agp.constant.MessageConstant;
import com.agp.constant.SMSConstant;
import com.agp.exception.AccountNotFoundException;
import lombok.Data;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

@Component
@ConfigurationProperties(prefix = "apg.sms")
@Data
public class SMSUtil {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取6位短信验证码
     *
     * @return
     */
    public String getSMSCode() {
        // 并发安全
        return String.valueOf(ThreadLocalRandom.current().nextInt(100000, 1000000));
    }

    /**
     * 发送验证码
     *
     */
    public void sentCaptcha(String phone,  String captcha){

        String host = "https://dfsns.market.alicloudapi.com";
        String path = "/data/send_sms";
        String method = "POST";
        String appcode = "1b6f542a53ca45aa8694bcc4f7b06482";
        Map<String, String> headers = new HashMap();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap();
        Map<String, String> bodys = new HashMap();
        bodys.put("content", "code:"+captcha);
        bodys.put("template_id", "CST_ptdie100");  //注意，CST_ptdie100该模板ID仅为调试使用，调试结果为"status": "OK" ，即表示接口调用成功，然后联系客服报备自己的专属签名模板ID，以保证短信稳定下发
        bodys.put("phone_number", phone);


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity,"UTF-8");
            System.out.println(result);
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 验证短信验证码
     * @param phone
     * @param captcha
     */
    public void checkCaptcha(String phone, String captcha){
        String redisCaptcha = (String) redisTemplate.opsForValue().get(SMSConstant.SMS_PHONE + phone);
        if (redisCaptcha == null || !captcha.equals(redisCaptcha)) {
            // CAPTCHA_ERROR:验证码错误
            throw new AccountNotFoundException(MessageConstant.CAPTCHA_ERROR);
        }
    }
}
